//为了调用路径规划
var QQMapWX = require('../../libs/qqmap-wx-jssdk');

var qqmapsdk;
//实例化API核心类，为了调用路径规划


// 实例化API核心类

var qqmapsdk = new QQMapWX({
  key: 'ZEOBZ-DW6LU-F66V2-2UZKW-TGXBS-5ABT3' // 必填
});
Page({
  data: {
    count: 1,
    markers: [{
        id: 1,
        longitude: 112.865492,
        latitude: 27.879396,
        name: 'move_point',
        alpha: 0.75,
        width: 15,
        height: 25

      },
      {
        id: 2,
        longitude: 112.865492,
        latitude: 27.879396,
        name: '起点',
        width: 15,
        height: 25,
        callout: {

          content: "下午1:30从金翰林公寓5栋出发",
          borderRadius: 15,
          padding: 10,
          display: 'BYCLICK',
          fontSize: 15,
          textAlign: "center",
          anchorX: -10
        },
      },
      {
        id: 3,
        longitude: 112.86384165287018,
        latitude: 27.87969036299283,
        name: '途径点1',
        width: 15,
        height: 25,
        callout: {

          content: "下午1:50从经过湘潭大学医院",
          borderRadius: 15,
          padding: 10,
          display: 'BYCLICK',
          fontSize: 15,
          textAlign: "center",
          anchorX: -10
        },
      },
      {
        id: 4,
        longitude: 112.86076700000001,
        latitude: 27.878277,
        name: '终点',
        width: 15,
        height: 25,
        callout: {
          content: "下午2:00到达兴湘教学楼C202上课",
          borderRadius: 15,
          padding: 10,
          display: 'BYCLICK',
          fontSize: 15,
          textAlign: "center",
          anchorX: -10
        },
      },
    ],
    polyline: [],


  },
  showInfo1:function (e) {
    if(this.data.count<2)
    return
    console.log("上一条" + this.data.count)
    this.setData({
      count: this.data.count -1,
    })
    this.setData({
      ['markers[' + this.data.count + '].callout.display']: 'BYCLICK',
    })
    var temp=this.data.count-1
    this.setData({
      ['markers[' + temp + '].callout.display']: 'ALWAYS',
    })
  

  },




  showInfo: function (e) {
    if(this.data.count>3)
    return;
    console.log("下一条" + this.data.count)
    if (this.data.count > 1)
    {  
        var temp=this.data.count-1
        this.setData({
        ['markers[' + temp + '].callout.display']: 'BYCLICK',
      })
    }


    this.setData({
      ['markers[' + this.data.count + '].callout.display']: 'ALWAYS',
      count: this.data.count + 1
    })

  },

  onLoad: function (options) {
    this.beginPolyline();
    qqmapsdk = new QQMapWX({
      key: 'ZEOBZ-DW6LU-F66V2-2UZKW-TGXBS-5ABT3'
    });
  },


  //moveAlong
  onReady: function (e) {
    //创建 map 上下文 MapContext 对象。
    this.mapCtx = wx.createMapContext('myMap')
  },
  moveAlong: function (e) {
    this.mapCtx.moveAlong({
      markerId: 1,
      path: this.data.polyline[0].points,
      duration: 2000,
      autoRotate: false
    })
  },


  /**
   * 页面的初始数据
   */
  beginPolyline() {
    var _this = this;
    //调用距离计算接口
    qqmapsdk.direction({
      mode: 'walking', //可选值：'driving'（驾车）、'walking'（步行）、'bicycling'（骑行），不填默认：'driving',可不填
      //from参数不填默认当前地址
      from: "27.879541,112.865461",
      to: "27.877935,112.860960",
      success: function (res) {
        console.log(res);
        var ret = res;
        var coors = ret.result.routes[0].polyline,
          pl = [];
        //坐标解压（返回的点串坐标，通过前向差分进行压缩）
        var kr = 1000000;
        for (var i = 2; i < coors.length; i++) {
          coors[i] = Number(coors[i - 2]) + Number(coors[i]) / kr;
        }
        //将解压后的坐标放入点串数组pl中
        for (var i = 0; i < coors.length; i += 2) {
          pl.push({
            latitude: coors[i],
            longitude: coors[i + 1]
          })
        }
        console.log(pl)
        //设置polyline属性，将路线显示出来,将解压坐标第一个数据作为起点
        _this.setData({
          latitude: pl[0].latitude,
          longitude: pl[0].longitude,
          polyline: [{
            points: pl,
            color: '#FF0000DD',
            width: 4
          }]
        })
      },
      fail: function (error) {
        console.error(error);
      },
      complete: function (res) {
        console.log(res);
      }
    });
  },



  /**
   * 生命周期函数--监听页面加载
   */

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})